//
//  DYInstallViewController.m
//  colorTest
//
//  Created by 丁益 on 2017/4/2.
//  Copyright © 2017年 celi. All rights reserved.
//  内置模式

#import "DYInstallViewController.h"
#import "SendWifiDataTool.h"

@interface DYInstallViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    SendWifiDataTool *sendWifiTool;
}

@property (nonatomic ,strong) NSMutableArray *installModeArray;
@property (nonatomic ,weak)   UIPickerView *pickerView;
@property (nonatomic ,assign) NSInteger currentRow;
@property (nonatomic ,assign) NSInteger modeIndex;
@end

@implementation DYInstallViewController

- (NSMutableArray *)installModeArray
{
    if (_installModeArray == nil) {
        _installModeArray = [NSMutableArray arrayWithObjects:@"模式1:红色渐变",@"模式2:蓝色渐变",@"模式3:绿色渐变",@"模式4:七彩渐变",@"模式5:循环闪烁", nil];
    }
    
    return _installModeArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.title = @"内置模式";
    self.view.backgroundColor = [UIColor whiteColor];
   
    // 选择框
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 20, 320, 216)];
   
    pickerView.centerX = sWidth/2;
    // 显示选中框
    pickerView.showsSelectionIndicator=YES;
    pickerView.dataSource = self;
    pickerView.delegate = self;
    self.pickerView = pickerView;
    [self.view addSubview:pickerView];

    // 设置显示模式下标
    self.modeIndex = 0;

    
    // 模式选择按钮
    CGFloat space = 20;
    CGFloat btnW = 40;
    CGFloat btnH = btnW;
    CGFloat addBtnY = CGRectGetMaxY(pickerView.frame)+10;
    UIButton *addBtn = [[UIButton alloc]initWithFrame:CGRectMake(space, addBtnY, btnW, btnH)];
    [addBtn setImage:[UIImage imageNamed:@"btnNext_n"] forState:UIControlStateNormal];
    [addBtn setImage:[UIImage imageNamed:@"btnNext_h"] forState:UIControlStateHighlighted];
    [addBtn addTarget:self action:@selector(addBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn];
    
    CGFloat subtractBtnX = sWidth - space - btnW;
    UIButton *subtractBtn = [[UIButton alloc]initWithFrame:CGRectMake(subtractBtnX, addBtnY, btnW, btnH)];
    [subtractBtn setImage:[UIImage imageNamed:@"btnPrev_n"] forState:UIControlStateNormal];
    [subtractBtn setImage:[UIImage imageNamed:@"btnPrev_h"] forState:UIControlStateHighlighted];
    [subtractBtn addTarget:self action:@selector(subtractBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:subtractBtn];
    
    
    CGFloat speedSliderY = CGRectGetMaxY(subtractBtn.frame);
    UISlider *speedSlider = [[UISlider alloc]initWithFrame:CGRectMake(30, speedSliderY,sWidth-60, 20)];
    speedSlider.value = 0.5;
    [speedSlider addTarget:self action:@selector(speedSliderChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:speedSlider];
    
    CGFloat titelLabelY = CGRectGetMaxY(speedSlider.frame)+5;
    UILabel *titelLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, titelLabelY, 40, 20)];
    titelLabel.centerX = sWidth/2;
    [titelLabel setText:@"速度"];
    [self.view addSubview: titelLabel];
    
    sendWifiTool = [SendWifiDataTool sharedSendWifiDataTool];
    
}


#pragma mark 按键事件
- (void)addBtnOnclick:(UIButton *)btn
{
    DLog(@"addBtnOnclick----");
    self.modeIndex++;
    if (self.modeIndex >= self.installModeArray.count) {
        self.modeIndex = self.installModeArray.count;
    }
    [sendWifiTool sendInsetCmd:self.modeIndex];
    [self.pickerView selectRow:self.modeIndex inComponent:0 animated:YES];
}

- (void)subtractBtnOnclick:(UIButton *)btn
{
    self.modeIndex--;
    if (self.modeIndex <= 0) {
        self.modeIndex = 0;
    }
    [sendWifiTool sendInsetCmd:self.modeIndex];
    [self.pickerView selectRow:self.modeIndex inComponent:0 animated:YES];
    DLog(@"subtractBtnOnclick");
}

#pragma mark speedSliderChanged事件
- (void)speedSliderChanged:(UISlider *)slider
{
    DLog(@"speedSliderChanged = %f",slider.value);
    
}
#pragma Mark -- UIPickerViewDataSource
// pickerView 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

// pickerView 每列个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return  self.installModeArray.count;
}

#pragma Mark -- UIPickerViewDelegate
// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 180;
}
// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    DLog(@"选择模式---%@",self.installModeArray[row]);
    
    [sendWifiTool sendInsetCmd:row];
}

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.installModeArray objectAtIndex:row];
}


@end
